package com.cmcc.cmii.smsim.handler;

import com.cmcc.cmii.smsim.constants.LayIMConstants;
import com.cmcc.cmii.smsim.entity.User;
import com.cmcc.cmii.smsim.entity.UserBigGroup;
import com.cmcc.cmii.smsim.service.IUserBigGroupService;
import com.cmcc.cmii.smsim.service.IUserService;
import com.cmcc.cmii.smsim.utils.jwt.JwtUtil;
import com.cmcc.cmii.smsim.utils.jwt.JwtVertifyResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.common.HttpResponseStatus;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.server.handler.IWsMsgHandler;

import java.util.List;

/**
 * @author Thanks
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyWebSocketMsgHandler implements IWsMsgHandler {
    private final IUserService iUserService;

    private final IUserBigGroupService iUserBigGroupService;
    /**
     * 握手
     * @param httpRequest
     * @param httpResponse
     * @param channelContext
     * @return
     * @throws Exception
     */
    @Override
    public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
        String accessToken = httpRequest.getParam("access_token");
        System.out.println("================接收到参数accessToken=========="+accessToken);
        JwtVertifyResult result = JwtUtil.verifyToken(accessToken);
        if (result.isVertified()) {
            bindUser(channelContext, result.getUserId());
        } else {
            httpResponse.setStatus(HttpResponseStatus.C401);
        }
        return httpResponse;
    }

    @Override
    public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {

        System.out.println("握手成功");
    }

    @Override
    public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
        System.out.println("接收到bytes消息");
        return null;
    }

    @Override
    public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
        return null;
    }

    @Override
    public Object onText(WsRequest wsRequest, String s, ChannelContext channelContext) throws Exception {
        System.out.println("接收到文本消息："+s);
        return null;
    }

    private void bindUser(ChannelContext channelContext, Long userId) {
        //绑定用户
        Tio.bindUser(channelContext, userId.toString());
        //绑定User属性信息
        User user = iUserService.queryById(userId);
        channelContext.setAttribute(LayIMConstants.CURRENT_USER_ATTRIBUTE, user);
        //绑定群组
        List<UserBigGroup> userBigGroups = iUserBigGroupService.queryListByWhere(UserBigGroup.builder().uid(userId).build());
        userBigGroups.forEach(userBigGroup -> {
            Tio.bindGroup(channelContext, userBigGroup.getGroupId().toString());
        });
    }
}
